package cn.anecansaitin.inet.mixins;

import cn.anecansaitin.inet.TranslateHelper;
import mcjty.rftoolsbase.api.xnet.gui.IEditorGui;
import mcjty.rftoolsbase.api.xnet.helper.AbstractConnectorSettings;
import mcjty.xnet.apiimpl.fluids.FluidConnectorSettings;
import mcjty.xnet.setup.Config;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(FluidConnectorSettings.class)
public abstract class FluidSettingMixin extends AbstractConnectorSettings {
    public FluidSettingMixin(@NotNull Direction side) {
        super(side);
    }

    @Shadow
    private FluidConnectorSettings.FluidMode fluidMode;
    @Shadow
    private Integer priority;
    @Shadow
    private Integer rate;
    @Shadow
    private Integer minmax;
    @Shadow
    private int speed;
    @Shadow
    private ItemStack filter;

    @Inject(method = "createGui", at = @At("HEAD"), remap = false, cancellable = true)
    public void mixin$createGui(IEditorGui gui, CallbackInfo ci) {
        advanced = gui.isAdvanced();
        String[] speeds;
        int maxrate;
        if (advanced) {
            speeds = new String[]{"10", "20", "60", "100", "200"};
            maxrate = Config.maxFluidRateAdvanced.get();
        } else {
            speeds = new String[]{"20", "60", "100", "200"};
            maxrate = Config.maxFluidRateNormal.get();
        }

        sideGui(gui);
        colorsGui(gui);
        redstoneGui(gui);
        gui.nl()
                .choices(FluidConnectorSettings.TAG_MODE, TranslateHelper.getText("inet.controller.setting.button.io.tooltip"), fluidMode, FluidConnectorSettings.FluidMode.values())
                .choices(FluidConnectorSettings.TAG_SPEED, TranslateHelper.getText("inet.controller.setting.button.speed.tooltip1"), Integer.toString(speed * 10), speeds)
                .nl()

                .label(TranslateHelper.getText("inet.controller.setting.label.pri.text")).integer(FluidConnectorSettings.TAG_PRIORITY, TranslateHelper.getText("inet.controller.setting.label.pri.tooltip"), priority, 36).nl()

                .label(TranslateHelper.getText("inet.controller.setting.label.rate.text"))
                .integer(FluidConnectorSettings.TAG_RATE, fluidMode == FluidConnectorSettings.FluidMode.EXT ? TranslateHelper.getText("inet.controller.setting.label.rate.fluid.tooltip1") + "|" + TranslateHelper.getText("inet.controller.setting.label.rate.fluid.tooltip3", maxrate) : TranslateHelper.getText("inet.controller.setting.label.rate.fluid.tooltip2") + "|" + TranslateHelper.getText("inet.controller.setting.label.rate.fluid.tooltip3", maxrate), rate, 36, maxrate)
                .shift(10)
                .label(fluidMode == FluidConnectorSettings.FluidMode.EXT ? "Min" : "Max")
                .integer(FluidConnectorSettings.TAG_MINMAX, fluidMode == FluidConnectorSettings.FluidMode.EXT ? TranslateHelper.getText("inet.controller.setting.label.min.fluid.tooltip") : TranslateHelper.getText("inet.controller.setting.label.max.fluid.tooltip"), minmax, 36)
                .nl()
                .label(TranslateHelper.getText("inet.controller.setting.item.filter.name"))
                .ghostSlot(FluidConnectorSettings.TAG_FILTER, filter);

        ci.cancel();
    }
}
